﻿/*=================================================================
* 版权信息：Copyright (C) 2005-2011 捷点科技 All Rights Reserved.
* 文件名称：CNVP_UnitsInfo.cs
* 创建标识：曾乃强 15158756652 2011-03-18 01:03:05
* 修改标识：
* 功能描述：
===================================================================*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using CNVP.Framework.Helper;
using System.Xml;
using CNVP.Framework.Utils;
using CNVP.Config;
using System.Web;

namespace CNVP.JShop.BLL 
{
    /// <summary>
    /// 对*CNVP_UnitsInfo*操作(针对业务层)的实现
    /// </summary>
    public class CNVP_UnitsInfo
    {
        #region "属性"
        /// <summary>
        /// 单元序号
        /// </summary>
        public int UnitID { get; set; }
        /// <summary>
        /// 栏目序号
        /// </summary>
        public int ColumnID { get; set; }
        /// <summary>
        /// 栏目类型(0为网站，1为栏目)
        /// </summary>
        public int UnitType { get; set; }
        /// <summary>
        /// 样式序号
        /// </summary>
        public int StyleID { get; set; }
        /// <summary>
        /// 单元名称
        /// </summary>
        public string UnitName { get; set; }
        /// <summary>
        /// 单元展示HTML代码
        /// </summary>
        public string UnitHtml { get; set; }
        #endregion
        private string StrSql = "";
        private string Prefix = DBConfig.Prefix;
        #region "读取单元"
        /// <summary>
        /// 读取单元
        /// </summary>
        /// <param name="UnitID"></param>
        /// <returns></returns>
        public DataTable GetUnitStyle(int UnitID)
        {
            StrSql = "Select * From " + Prefix + "UnitsInfo Where UnitID=@UnitID";
            SqlParameter[] param = { DbHelper.MakeInParam("@UnitID", SqlDbType.Int, 4, UnitID) };
            return DbHelper.ExecuteTable(StrSql, param);
        }
        /// <summary>
        /// 读取单元风格
        /// </summary>
        /// <param name="UnitID"></param>
        /// <param name="UnitID"></param>
        /// <returns></returns>
        public DataTable GetUnitStyle(int UnitID, int StyleID)
        {
            StrSql = "Select * From " + Prefix + "UnitsInfo Where UnitID=@UnitID And StyleID=@StyleID";
            SqlParameter[] param = {  DbHelper.MakeInParam("@UnitID", SqlDbType.Int, 4, UnitID),
                                      DbHelper.MakeInParam("@StyleID", SqlDbType.Int, 4, StyleID),
                                   };
            return DbHelper.ExecuteTable(StrSql, param);
        }
        /// <summary>
        /// 读取单元风格
        /// </summary>
        /// <param name="UnitName"></param>
        /// <param name="TemplateDir"></param>
        /// <returns></returns>
        public DataTable GetUnitStyle(string UnitName, string ProjectName)
        {
            StrSql = "Select * From " + Prefix + "UnitsInfo Where UnitName=@UnitName And ProjectName=@ProjectName";
            SqlParameter[] param = {  DbHelper.MakeInParam("@UnitName",SqlDbType.VarChar,50,UnitName),
                                      DbHelper.MakeInParam("@ProjectName",SqlDbType.VarChar,50,ProjectName),
                                   };
            return DbHelper.ExecuteTable(StrSql, param);
        }
        #endregion
        #region "更新单元"
        /// <summary>
        /// 增加新的单元
        /// </summary>
        /// <param name="UnitName"></param>
        /// <param name="UnitID"></param>
        public void AddUnitStyle(string UnitName, string ProjectName, out int UnitID)
        {
            StrSql = "Insert Into " + Prefix + "UnitsInfo (ProjectName,UnitName,StyleID) Values (@ProjectName,@UnitName,0);SELECT @@IDENTITY As NewID";
            SqlParameter[] param = {   DbHelper.MakeInParam("@ProjectName",SqlDbType.VarChar,50,ProjectName),
                                       DbHelper.MakeInParam("@UnitName",SqlDbType.VarChar,50,UnitName)
                                   };
            UnitID = Convert.ToInt32(DbHelper.ExecuteScalar(StrSql, param));
        }
        /// <summary>
        /// 更新单元状态
        /// </summary>
        /// <param name="ColumnID"></param>
        /// <param name="UnitID"></param>
        /// <param name="UnitState"></param>
        public void EditUnitStyleState(int ColumnID, int UnitID, string UnitState)
        {
            StrSql = "Update " + Prefix + "UnitsInfo Set UnitState=@UnitState Where ColumnID=@ColumnID And UnitID=@UnitID";
            SqlParameter[] param = {   DbHelper.MakeInParam("@ColumnID",SqlDbType.Int,4,ColumnID),
                                       DbHelper.MakeInParam("@UnitID",SqlDbType.Int,4,UnitID),
                                       DbHelper.MakeInParam("@UnitState",SqlDbType.VarChar,50,UnitState)
                                   };
            DbHelper.ExecuteNonQuery(StrSql, param);
        }
        /// <summary>
        /// 编辑单元内容
        /// </summary>
        public void EditUnitStyle()
        {
            StrSql = "Update " + Prefix + "UnitsInfo Set StyleID=@StyleID,UnitHtml=@UnitHtml Where UnitID=@UnitID";
            SqlParameter[] param = {  DbHelper.MakeInParam("@StyleID",SqlDbType.Int,4,StyleID),
                                      DbHelper.MakeInParam("@UnitHtml",SqlDbType.Text,0,UnitHtml),
                                      DbHelper.MakeInParam("@UnitID",SqlDbType.Int,4,UnitID)
                                   };
            DbHelper.ExecuteNonQuery(StrSql, param);
        }
        /// <summary>
        /// 单元内容初始化
        /// </summary>
        public void RestUnitStyle(int UnitID)
        {
            StrSql = "Update " + Prefix + "UnitsInfo Set StyleID=0,UnitHTML=NULL Where UnitID=@UnitID";
            SqlParameter[] param = { DbHelper.MakeInParam("@UnitID", SqlDbType.Int, 4, UnitID) };
            DbHelper.ExecuteNonQuery(StrSql, param);
        }
        public void RestUnitStyle()
        {
            StrSql = "Update " + Prefix + "UnitsInfo Set StyleID=0,UnitHTML=NULL Where UnitID=@UnitID";
            SqlParameter[] param = { DbHelper.MakeInParam("@UnitID", SqlDbType.Int, 4,this.UnitID) };
            DbHelper.ExecuteNonQuery(StrSql, param);
        }
        #endregion
        #region "删除无效单元"
        /// <summary>
        /// 删除无效单元
        /// </summary>
        /// <param name="ColumnID"></param>
        /// <param name="UnitState"></param>
        public void DelUnitStyle(int ColumnID, string UnitState)
        {
            StrSql = "Delete From " + Prefix + "UnitsInfo Where ColumnID=@ColumnID And UnitState<>@UnitState";
            SqlParameter[] param = {   DbHelper.MakeInParam("ColumnID",SqlDbType.Int,4,ColumnID),
                                       DbHelper.MakeInParam("@UnitState",SqlDbType.VarChar,50,UnitState)
                                   };
            DbHelper.ExecuteNonQuery(StrSql, param);
        }
        #endregion
        #region "读取样式属性"
        /// <summary>
        /// 读取样式属性
        /// </summary>
        /// <returns></returns>
        public DataTable GetUnitStyleProperty()
        {
            if (CacheUtils.Get("UnitStyle") == null)
            {
                string FileName = HttpContext.Current.Server.MapPath(UIConfig.InstallDir + UIConfig.AdminDir + "/Template/Template.config");
                if (!FileUtils.FileExists(FileName))
                {
                    LogHelper.Write("配置文件读取失败", "Template.config读取错误。");
                    return null;
                }
                XmlDocument Xml = new XmlDocument();
                Xml.Load(FileName);
                XmlNodeList RootList = Xml.SelectNodes("CNVP/Property/Item");

                DataTable _Dt = new DataTable("Result");
                _Dt.Columns.Add("ItemName", typeof(string));
                _Dt.Columns.Add("ItemValue", typeof(string));
                _Dt.Columns.Add("ItemUrl", typeof(string));
                _Dt.Columns.Add("ItemType", typeof(string));

                foreach (XmlNode Xn in RootList)
                {
                    DataRow _Rows = _Dt.NewRow();
                    _Rows["ItemName"] = Xn.Attributes["ItemName"].InnerText;
                    _Rows["ItemValue"] = Xn.Attributes["ItemValue"].InnerText;
                    _Rows["ItemUrl"] = Xn.Attributes["ItemUrl"].InnerText;
                    _Rows["ItemType"] = Xn.Attributes["ItemType"].InnerText;
                    _Dt.Rows.Add(_Rows);
                }
                CacheUtils.Insert("UnitStyle", _Dt);
                return _Dt;
            }
            else
            {
                return (DataTable)CacheUtils.Get("UnitStyle");
            }
        }
        public DataTable GetUnitStyleProperty(int templateID)
        {
            if (CacheUtils.Get("UnitStyle" + templateID) == null)
            {
                string FileName = HttpContext.Current.Server.MapPath(UIConfig.InstallDir + UIConfig.AdminDir + "/Template/Template.config");
                if (!FileUtils.FileExists(FileName))
                {
                    LogHelper.Write("配置文件读取失败", "Template.config读取错误。");
                    return null;
                }
                XmlDocument Xml = new XmlDocument();
                Xml.Load(FileName);
                XmlNodeList RootList = Xml.SelectNodes("CNVP/Property/Item[@ItemValue=" + templateID + "]");

                DataTable _Dt = new DataTable("Result");
                _Dt.Columns.Add("ItemName", typeof(string));
                _Dt.Columns.Add("ItemValue", typeof(string));
                _Dt.Columns.Add("ItemUrl", typeof(string));
                _Dt.Columns.Add("ItemType", typeof(string));

                foreach (XmlNode Xn in RootList)
                {
                    DataRow _Rows = _Dt.NewRow();
                    _Rows["ItemName"] = Xn.Attributes["ItemName"].InnerText;
                    _Rows["ItemValue"] = Xn.Attributes["ItemValue"].InnerText;
                    _Rows["ItemUrl"] = Xn.Attributes["ItemUrl"].InnerText;
                    _Rows["ItemType"] = Xn.Attributes["ItemType"].InnerText;
                    _Dt.Rows.Add(_Rows);
                }
                CacheUtils.Insert("UnitStyle" + templateID, _Dt);
                return _Dt;
            }
            else
            {
                return (DataTable)CacheUtils.Get("UnitStyle" + templateID);
            }
        }
        /// <summary>
        /// 读取样式属性
        /// </summary>
        /// <param name="ItemValue"></param>
        /// <param name="ItemType"></param>
        /// <returns></returns>
        public DataTable GetUnitStyleProperty(string ItemValue, string CurrentType)
        {
            DataTable Dt = GetUnitStyleProperty();
            DataTable _Dt = new DataTable("Result");
            _Dt.Columns.Add("ItemName", typeof(string));
            _Dt.Columns.Add("ItemValue", typeof(string));
            _Dt.Columns.Add("ItemUrl", typeof(string));
            _Dt.Columns.Add("ItemType", typeof(string));

            foreach (DataRow Row in Dt.Rows)
            {
                string _ItemType = Row["ItemType"].ToString();
                string _ItemValue = Row["ItemValue"].ToString();
                if ((ItemValue == _ItemValue) | (string.IsNullOrEmpty(_ItemType)) | (UnitStyleCanUse(_ItemType, CurrentType)))
                {
                    DataRow _Rows = _Dt.NewRow();
                    _Rows["ItemName"] = Row["ItemName"].ToString();
                    _Rows["ItemValue"] = Row["ItemValue"].ToString();
                    _Rows["ItemUrl"] = Row["ItemUrl"].ToString();
                    _Rows["ItemType"] = Row["ItemType"].ToString();
                    _Dt.Rows.Add(_Rows);
                }
            }
            return _Dt;
        }
        /// <summary>
        /// 判断是否可用
        /// </summary>
        /// <param name="ItemType"></param>
        /// <param name="CurrentType"></param>
        /// <returns></returns>
        private bool UnitStyleCanUse(string ItemType, string CurrentType)
        {
            bool flg = false;
            foreach (string a in ItemType.Split('|'))
            {
                if (a == CurrentType)
                {
                    flg = true;
                    break;
                }
            }
            return flg;
        }
        #endregion
        #region "读取HTML代码属性"
        /// <summary>
        /// 读取HTML代码属性
        /// </summary>
        /// <returns></returns>
        public DataTable GetHTMLCodeProperty()
        {
            if (CacheUtils.Get("HTMLCode") == null)
            {
                string FileName = HttpContext.Current.Server.MapPath(UIConfig.InstallDir + UIConfig.AdminDir + "/Template/HTMLCode.config");
                if (!FileUtils.FileExists(FileName))
                {
                    LogHelper.Write("配置文件读取失败", "HTMLCode.config读取错误。");
                    return null;
                }
                XmlDocument Xml = new XmlDocument();
                Xml.Load(FileName);
                XmlNodeList RootList = Xml.SelectNodes("CNVP/Property/Item");

                DataTable _Dt = new DataTable("Result");
                _Dt.Columns.Add("ItemName", typeof(string));
                _Dt.Columns.Add("ItemValue", typeof(string));

                foreach (XmlNode Xn in RootList)
                {
                    DataRow _Rows = _Dt.NewRow();
                    _Rows["ItemName"] = Xn.ChildNodes[0].InnerText;
                    _Rows["ItemValue"] = Xn.ChildNodes[1].InnerText;
                    _Dt.Rows.Add(_Rows);
                }
                CacheUtils.Insert("HTMLCode", _Dt);
                return _Dt;
            }
            else
            {
                return (DataTable)CacheUtils.Get("HTMLCode");
            }
        }
        #endregion
        #region "单元样式"
        #region "属性"
        /// <summary>
        /// 单元样式文件地址
        /// </summary>
        private string UnitStyleFileName = HttpContext.Current.Server.MapPath(UIConfig.InstallDir + UIConfig.AdminDir + "/Template/UnitStyle.config");
        /// <summary>
        /// 样式序号
        /// </summary>
        public string Type { get; set; }
        /// <summary>
        /// 样式名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 样式脚本
        /// </summary>
        public string Script { get; set; }
        /// <summary>
        /// 预览效果
        /// </summary>
        public string ShowHTML { get; set; }
        #endregion
        #region "单元样式列表"
        /// <summary>
        /// 单元样式
        /// </summary>
        /// <returns></returns>
        public DataTable UnitStyleXmlList()
        {
            XmlDocument Xml = new XmlDocument();
            Xml.Load(UnitStyleFileName);
            XmlNodeList RootList = Xml.SelectNodes("/cnvp/unit");
            DataTable _Dt = new DataTable("Result");
            _Dt.Columns.Add("ID", typeof(string));
            _Dt.Columns.Add("Type", typeof(string));
            _Dt.Columns.Add("Name", typeof(string));
            _Dt.Columns.Add("Script", typeof(string));
            _Dt.Columns.Add("ShowHTML", typeof(string));

            int i = 0;
            foreach (XmlNode Xn in RootList)
            {
                DataRow _Rows = _Dt.NewRow();
                _Rows["ID"] = Xn.Attributes["id"].Value;
                _Rows["Type"] = Xn.ChildNodes[0].InnerText;
                _Rows["Name"] = Xn.ChildNodes[1].InnerText;
                _Rows["Script"] = Xn.ChildNodes[2].InnerText;
                _Rows["ShowHTML"] = Xn.ChildNodes[3].InnerText;
                _Dt.Rows.Add(_Rows);
                i++;
            }
            return _Dt;
        }
        public bool IsAllowFile( string filename)
        {
            XmlDocument Xml = new XmlDocument();
            Xml.Load(HttpContext.Current.Server.MapPath(UIConfig.InstallDir + UIConfig.AdminDir + "/Template/Template.config"));
            XmlNode noot = Xml.SelectSingleNode("/CNVP/AllowAddFileType/Item[@ItemName=\"" + filename + "\"]");
            if (noot != null)
            {
                return true;
            }
            else
            { 
                return false; 
            }
        }
        public DataTable UnitStyleXmlList(int unitID )
        {
            XmlDocument Xml = new XmlDocument();
            Xml.Load(UnitStyleFileName);
            XmlNodeList RootList = Xml.SelectNodes(@"/cnvp/unit[type="+unitID+@"]");
            DataTable _Dt = new DataTable("Result");
            _Dt.Columns.Add("ID", typeof(string));
            _Dt.Columns.Add("Type", typeof(string));
            _Dt.Columns.Add("Name", typeof(string));
            _Dt.Columns.Add("Script", typeof(string));
            _Dt.Columns.Add("ShowHTML", typeof(string));

            foreach (XmlNode Xn in RootList)
            {
                DataRow _Rows = _Dt.NewRow();
                _Rows["ID"] = Xn.Attributes["id"].Value;
                _Rows["Type"] = Xn.ChildNodes[0].InnerText;
                _Rows["Name"] = Xn.ChildNodes[1].InnerText;
                _Rows["Script"] = Xn.ChildNodes[2].InnerText;
                _Rows["ShowHTML"] = Xn.ChildNodes[3].InnerText;
                _Dt.Rows.Add(_Rows);
            }
            return _Dt;
        }
        /// <summary>
        /// 获取单一单元样式
        /// </summary>
        /// <returns></returns>
        public DataTable UnitStyleXmlSingle(int styleID)
        {
            XmlDocument Xml = new XmlDocument();
            Xml.Load(UnitStyleFileName);
            XmlNodeList RootList = Xml.SelectNodes("cnvp/unit[@id=" + styleID + "]");

            DataTable _Dt = new DataTable("Result");
            _Dt.Columns.Add("ID", typeof(string));
            _Dt.Columns.Add("Type", typeof(string));
            _Dt.Columns.Add("Name", typeof(string));
            _Dt.Columns.Add("Script", typeof(string));
            _Dt.Columns.Add("ShowHTML", typeof(string));

            DataRow _Rows = _Dt.NewRow();
            _Rows["ID"] = styleID;
            _Rows["Type"] = RootList.Item(StyleID).ChildNodes[0].InnerText;
            _Rows["Name"] = RootList.Item(StyleID).ChildNodes[1].InnerText;
            _Rows["Script"] = RootList.Item(StyleID).ChildNodes[2].InnerText;
            _Rows["ShowHTML"] = RootList.Item(StyleID).ChildNodes[3].InnerText;
            _Dt.Rows.Add(_Rows);

            return _Dt;
        }
        #endregion
        #region "添加单元样式"
        /// <summary>
        /// 添加单元样式
        /// </summary>
        public void UnitStyleXmlAdd()
        {
            XmlDocument Xml = new XmlDocument();
            Xml.Load(UnitStyleFileName);
            XmlCDataSection CData;

            XmlNode Root = Xml.SelectSingleNode("cnvp");
            XmlElement Xe1 = Xml.CreateElement("unit");

            XmlElement XeSub1 = Xml.CreateElement("type");
            XeSub1.InnerText = Type;
            Xe1.AppendChild(XeSub1);
            XmlElement XeSub2 = Xml.CreateElement("name");
            XeSub2.InnerText = Name;
            Xe1.AppendChild(XeSub2);
            XmlElement XeSub3 = Xml.CreateElement("script");
            CData = Xml.CreateCDataSection(Script);
            XeSub3.AppendChild(CData);
            Xe1.AppendChild(XeSub3);
            XmlElement XeSub4 = Xml.CreateElement("showhtml");
            CData = Xml.CreateCDataSection(ShowHTML);
            XeSub4.AppendChild(CData);
            Xe1.AppendChild(XeSub4);

            Root.AppendChild(Xe1);
            Xml.Save(UnitStyleFileName);
        }
        #endregion
        #region "编辑单元样式"
        /// <summary>
        /// 编辑单元样式
        /// </summary>
        public void UnitStyleXmlEdit(int ID)
        {
            XmlDocument Xml = new XmlDocument();
            Xml.Load(UnitStyleFileName);
            XmlCDataSection CData;

            XmlNodeList RootList = Xml.SelectNodes("cnvp/unit");

            RootList.Item(ID).ChildNodes[1].InnerText = Name;
            CData = Xml.CreateCDataSection(Script);
            RootList.Item(ID).ChildNodes[2].InnerText = "";
            RootList.Item(ID).ChildNodes[2].AppendChild(CData);
            CData = Xml.CreateCDataSection(ShowHTML);
            RootList.Item(ID).ChildNodes[3].InnerText = "";
            RootList.Item(ID).ChildNodes[3].AppendChild(CData);

            Xml.Save(UnitStyleFileName);
        }
        #endregion
        #endregion
        #region "复制单元"
        /// <summary>
        /// 复制单元
        /// </summary>
        /// <param name="FormUnitID"></param>
        /// <param name="ToUnitID"></param>
        public void CopyUnitStyle(int FormUnitID, int ToUnitID)
        {
            DataTable Dt = GetUnitStyle(FormUnitID);
            if (Dt != null && Dt.Rows.Count > 0)
            {
                StyleID = Convert.ToInt32(Dt.Rows[0]["StyleID"]);
                UnitHtml = Dt.Rows[0]["UnitHtml"].ToString();
                UnitID = ToUnitID;
                this.EditUnitStyle();
            }
            Dt.Dispose();
            Dt.Clear();
        }
        #endregion
        #region "剪切单元"
        /// <summary>
        /// 剪切单元
        /// </summary>
        /// <param name="FormUnitID"></param>
        /// <param name="ToUnitID"></param>
        public void CutUnitStyle(int FormUnitID, int ToUnitID)
        {
            DataTable Dt = GetUnitStyle(FormUnitID);
            if (Dt != null && Dt.Rows.Count > 0)
            {
                StyleID = Convert.ToInt32(Dt.Rows[0]["StyleID"]);
                UnitHtml = Dt.Rows[0]["UnitHtml"].ToString();
                UnitID = ToUnitID;
                this.EditUnitStyle();
            }
            Dt.Dispose();
            Dt.Clear();
            DelUnitStyle(FormUnitID);
        }
        #endregion
        #region "删除单元"
        /// <summary>
        /// 删除单元
        /// </summary>
        /// <param name="UnitID"></param>
        public void DelUnitStyle(int UnitID)
        {
            RestUnitStyle(UnitID);
        }
        #endregion

        DAL.CNVP_UnitsInfo dal = new DAL.CNVP_UnitsInfo();
        /// <summary>
        /// 查看全部
        /// </summary>
        public List<Model.CNVP_UnitsInfo> GetAll_CNVP_UnitsInfo(string sqlWhere)
        {
            return dal.LoadAll(sqlWhere);
        }
        /// <summary>
        /// 查看全部
        /// </summary>
        public List<Model.CNVP_UnitsInfo> GetAll_CNVP_UnitsInfo()
        {
            return this.GetAll_CNVP_UnitsInfo("");
        }

        /// <summary>
        /// 删除
        /// </summary>
        public int Delete_CNVP_UnitsInfo(Model.CNVP_UnitsInfo obj)
        {
            return dal.Delete(obj);
        }

        /// <summary>
        /// 添加
        /// </summary>
        public int Add_CNVP_UnitsInfo(Model.CNVP_UnitsInfo obj)
        {
            return dal.Insert(obj);
        }

    }
}

